package com.kyomonn.talkischeap.PersonalCenter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.kyomonn.talkischeap.CodingResources.CodingResourcesFragment;
import com.kyomonn.talkischeap.LoginActivity;
import com.kyomonn.talkischeap.R;

public class PersonalCenterFragment extends Fragment {

    private SharedPreferences sharedPref;

    public static PersonalCenterFragment Create() {
        return new PersonalCenterFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        sharedPref = requireContext().getSharedPreferences("user_data", Context.MODE_PRIVATE);

        if (!sharedPref.getBoolean("is_login", false)) {
            new AlertDialog.Builder(requireContext())
                    .setTitle("您还没有登录")
                    .setMessage("点击确定跳转到登录界面")
                    .setPositiveButton("确定", (dialog, which) -> startActivity(new Intent(requireContext(), LoginActivity.class)))
                    .setNegativeButton("取消", (dialog, which) -> {
                        CodingResourcesFragment codingResourcesFragment = new CodingResourcesFragment();
                        FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fragment_container_view, codingResourcesFragment);
                        transaction.commit();
                    })
                    .show();
            return null;
        }

        return inflater.inflate(R.layout.fragment_personal_center, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView tvUsername = view.findViewById(R.id.tv_user_name);
        TextView tvUserEmail = view.findViewById(R.id.tv_user_email);
        ImageView ivHeadshot = view.findViewById(R.id.iv_headshot_personal_fragment);

        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("联系方式");
        builder.setMessage("QQ：1234567890\n邮箱：qwer@123.com");
        builder.setPositiveButton("跳转QQ", (dialog, which) -> {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mqq://"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(requireContext(), "未找到QQ应用", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("跳转邮箱", (dialog, which) -> {
            try {
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "qwer@123.com", null));
                emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(emailIntent);
            } catch (Exception e) {
                Toast.makeText(requireContext(), "未找到邮件客户端", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("取消", (dialog, which) -> dialog.dismiss());

        view.findViewById(R.id.btn_personal_center).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireContext(), PersonalCenterActivity.class));
            }
        });

        view.findViewById(R.id.btn_contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.show();
            }
        });

        String username = sharedPref.getString("username", "");
        String email = sharedPref.getString("email", "");
        boolean isLogin = sharedPref.getBoolean("is_login", false);

        if (isLogin) {
            tvUsername.setText(username);
            tvUserEmail.setText(email);
//            ivHeadshot.setImageResource(R.drawable.headshot);
        } else {
            tvUsername.setOnClickListener(v -> toLoginActivity());
            ivHeadshot.setOnClickListener(v -> toLoginActivity());
        }
    }

    private void toLoginActivity() {
        startActivity(new Intent(requireContext(), LoginActivity.class));
    }
}